
package com.gomefinance;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    
    public final static Log log = LogFactory.getLog(StringUtils.class);

    public static String convertQuot(String s) {
        return s.replace("'", "\\'").replace("\"", "\\\"");
    }

    /**
     * 逗号相连的数字字符串，使用逗号分隔开，转为List存放到List容器中
     * 
     * @param
     * @return list
     */
    public static List<Long> convertStrToLongList(String str) {
        List<Long> list = new ArrayList<Long>();
        String[] arrs = org.apache.commons.lang.StringUtils.split(str, ",");
        for (String value : arrs) {
            if (NumberUtils.isDigits(value)) {
                list.add(Long.valueOf(value));
            }
        }
        return list;
    }

    public static synchronized String encryptSha256(String s) {
        try {
            MessageDigest messagedigest = MessageDigest.getInstance("SHA-256");
            byte abyte0[] = messagedigest.digest(s.getBytes("UTF-8"));
            return new String(Base64.encodeBase64(abyte0));
        } catch (Exception exception) {
            return null;
        }
    }

    public static StringBuilder formatMsg(CharSequence charsequence, boolean flag, Object aobj[]) {
        int i = aobj.length;
        boolean flag1 = false;
        StringBuilder stringbuilder = new StringBuilder(charsequence);
        if (i > 0) {
            for (int j = 0; j < i; j++) {
                String s = new StringBuilder().append("%").append(j + 1).toString();
                for (int k = stringbuilder.indexOf(s); k >= 0; k = stringbuilder.indexOf(s)) {
                    flag1 = true;
                    stringbuilder.replace(k, k + 2, toString(aobj[j], flag));
                }

            }

            if (aobj[i - 1] instanceof Throwable) {
                StringWriter stringwriter = new StringWriter();
                ((Throwable) aobj[i - 1]).printStackTrace(new PrintWriter(stringwriter));
                stringbuilder.append("\n").append(stringwriter.toString());
            } else if (i == 1 && !flag1) {
                stringbuilder.append(aobj[i - 1].toString());
            }
        }
        return stringbuilder;
    }

    public static StringBuilder formatMsg(String s, Object aobj[]) {
        return formatMsg(new StringBuilder(s), true, aobj);
    }

    public static String html2Text(String s) {
        String s1 = s;
        String s2 = "";
        try {
            String s3 = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
            String s4 = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
            String s5 = "<[^>]+>";
            Pattern pattern = Pattern.compile(s3, 2);
            Matcher matcher = pattern.matcher(s1);
            s1 = matcher.replaceAll("");
            Pattern pattern1 = Pattern.compile(s4, 2);
            Matcher matcher1 = pattern1.matcher(s1);
            s1 = matcher1.replaceAll("");
            Pattern pattern2 = Pattern.compile(s5, 2);
            Matcher matcher2 = pattern2.matcher(s1);
            s1 = matcher2.replaceAll("");
            s2 = s1;
        } catch (Exception exception) {
            System.err.println(new StringBuilder().append("Html2Text: ").append(exception.getMessage()).toString());
        }
        return s2;
    }

    public static String htmlEntityToString(String s) {
        int i = 0;
        StringBuffer stringbuffer = new StringBuffer();
        do {
            if (i <= -1) {
                break;
            }
            byte byte0 = 10;
            if (i == 0) {
                int k = s.indexOf("&#");
                if (i != k) {
                    i = k;
                }
            }
            int j = s.indexOf(";", i + 2);
            String s1 = "";
            if (j != -1) {
                s1 = s.substring(i + 2, j);
                char c = s1.charAt(0);
                if (c == 'x' || c == 'X') {
                    byte0 = 16;
                    s1 = s1.substring(1);
                }
            }
            try {
                char c1 = (char) Integer.parseInt(s1, byte0);
                stringbuffer.append(new Character(c1).toString());
            } catch (NumberFormatException numberformatexception) {
                numberformatexception.printStackTrace();
            }
            i = s.indexOf("&#", j);
            if (i - j > 1) {
                stringbuffer.append(s.substring(j + 1, i));
            }
            if (i == -1) {
                int l = s.length();
                if (j + 1 != l) {
                    stringbuffer.append(s.substring(j + 1, l));
                }
            }
        } while (true);
        return stringbuffer.toString();
    }

    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if (o instanceof String) {
            if (((String) o).trim().length() == 0) {
                return true;
            }
        }
        if (o instanceof Collection) {
            if (!((Collection) o).isEmpty()) {
                return true;
            }
        }
        if (o.getClass().isArray()) {
            if (((Object[]) o).length != 0) {
                return true;
            }
        }

        if (o instanceof Map) {
            if (!((Map) o).isEmpty()) {
                return true;
            }
        }

        if (o instanceof Long) {
            if ((Long) o != null) {
                return true;
            }
        }

        if (o instanceof Short) {
            if ((Short) o != null) {
                return true;
            }
        }

        return false;
    }

    public static boolean isNotEmpty(Long o) {
        return !isEmpty(o);
    }

    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    public static boolean isNumeric(String s) {
        for (int i = s.length(); --i >= 0;) {
            if (!Character.isDigit(s.charAt(i))) {
                return false;
            }
        }

        return true;
    }

    public static String stringToHtmlEntity(String s) {
        StringBuffer stringbuffer = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case 10: // '\n'
                    stringbuffer.append(c);
                    break;

                case 60: // '<'
                    stringbuffer.append("&lt;");
                    break;

                case 62: // '>'
                    stringbuffer.append("&gt;");
                    break;

                case 38: // '&'
                    stringbuffer.append("&amp;");
                    break;

                case 39: // '\''
                    stringbuffer.append("&apos;");
                    break;

                case 34: // '"'
                    stringbuffer.append("&quot;");
                    break;

                default:
                    if (c < ' ' || c > '~') {
                        stringbuffer.append("&#x");
                        stringbuffer.append(Integer.toString(c, 16));
                        stringbuffer.append(';');
                    } else {
                        stringbuffer.append(c);
                    }
                    break;
            }
        }

        return stringbuffer.toString();
    }

    public static String stringToUnicode(String s) {
        String s1 = "";
        char ac[] = new char[s.length()];
        for (int i = 0; i < ac.length; i++) {
            ac[i] = s.charAt(i);
            s1 = new StringBuilder().append(s1).append("\\u").append(Integer.toString(ac[i], 16)).toString();
        }

        return s1;
    }

    public static String toString(Object obj, boolean flag) {
        StringBuilder stringbuilder = new StringBuilder();
        if (obj == null) {
            stringbuilder.append("NULL");
        } else if (obj instanceof Object[]) {
            for (int i = 0; i < ((Object[]) obj).length; i++) {
                stringbuilder.append(((Object[]) obj)[i]).append(", ");
            }

            if (stringbuilder.length() > 0) {
                stringbuilder.delete(stringbuilder.length() - 2, stringbuilder.length());
            }
        } else {
            stringbuilder.append(obj.toString());
        }
        if (flag && stringbuilder.length() > 0
            && (stringbuilder.charAt(0) != '[' || stringbuilder.charAt(stringbuilder.length() - 1) != ']')
            && (stringbuilder.charAt(0) != '{' || stringbuilder.charAt(stringbuilder.length() - 1) != '}')) {
            stringbuilder.insert(0, "[").append("]");
        }
        return stringbuilder.toString();
    }

    public static String unicodeToString(String s) {
        StringBuffer stringbuffer = new StringBuffer();
        String as[] = s.toUpperCase().split("\\\\U");
        for (int i = 0; i < as.length; i++) {
            if (!as[i].equals("")) {
                char c = (char) Integer.parseInt(as[i].trim(), 16);
                stringbuffer.append(c);
            }
        }

        return stringbuffer.toString();
    }
    public static String firstCharUpperCase(String str) {
        char[] cs = str.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }
    
    public static String firstCharLowerCase(String str) {
        char[] cs = str.toCharArray();
        cs[0] += 32;
        return String.valueOf(cs);
    }
    /**
     * 转换为下划线命名法规则<br>
     * <code>String str = "userName";<br>
     * str = parse2Underscore(str);</code> <br>
     * 结果 str = "USER_NAME"
     * 
     * @param str
     * @return
     */
    public static String parse2Underscore(String str) {
        char[] chars = str.toCharArray();
        StringBuffer s = new StringBuffer();
        boolean isFirst = true;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] >= 65 && chars[i] <= 90) {
                if (!isFirst)
                    s.append('_');
                s.append(chars[i]);
            } else if (chars[i] >= 97 && chars[i] <= 122) {
                s.append((char) (chars[i] - 32));
            } else {
                s.append(chars[i]);
            }
            isFirst = false;
        }
        return s.toString();
    }
    /**
     * 转换成java变量命名法<br>
     * <code>String str = "USER_NAME";<br>
     * str = parse2JavaName(str);</code> <br>
     * 结果 str = "userName"
     * 
     * @param str
     * @return
     */
    public static String parse2JavaName(String str) {
        str = str.toLowerCase();
        char[] chars = str.toCharArray();
        char[] toChar = new char[chars.length];
        int j = 0;
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if ('_' == c) {
                i++;
                c = chars[i];
                c = (char) ((int) c - 32);
            }
            toChar[j] = c;
            j++;
        }
        return new String(toChar).trim();
    }
    /**
     * 字符串补齐
     * @param source 源字符串
     * @param fillLength 补齐长度
     * @param fillChar 补齐的字符
     * @param isLeftFill true为左补齐，false为右补齐
     * @return
     */
    public static String stringFill(String source, int fillLength, char fillChar, boolean isLeftFill) {
        if (source == null || source.length() >= fillLength) return source;

        StringBuilder result = new StringBuilder(fillLength);
        int len = fillLength - source.length();
        if (isLeftFill) {
            for (; len > 0; len--) {
                result.append(fillChar);
            }
            result.append(source);
        } else {
            result.append(source);
            for (; len > 0; len--) {
                result.append(fillChar);
            }
        }
        return result.toString();
    }

    /**
     * 去掉字符串中的换行符等
     * @param str
     * @return
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }


    public static void main(String[] args) {
        System.out.println(isEmpty(""));
    }


}
